using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTarget : MonoBehaviour
{
    private Transform target;//目标对象
    public float smoothSpeed = 8f;//平滑速度
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (target)
        {
            Vector3 targetPos = transform.position;
            targetPos.x = target.position.x;
            
            targetPos.x = Mathf.Clamp(targetPos.x, 0, 20);//限制x轴在屏幕内
            
            transform.position = Vector3.MoveTowards(transform.position, targetPos, Time.deltaTime * smoothSpeed);
        }
    }
    
    public void SetTarget(Transform transform)
    {
        target = transform;
    }
}
